package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;

import com.be.beadmin.device.dto.input.InspectPlanNewDto;
import com.be.beadmin.device.dto.output.InspectPlanEx;
import com.be.beadmin.device.dto.output.InspectPlanExcelEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import com.be.beadmin.device.service.InspectPlanService;
import com.be.beadmin.device.dto.input.InspectPlanDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author chenxu
* @date 2023-09-18
**/
@RestController
public class InspectPlanController {

    @Autowired
    private  InspectPlanService inspectPlanService;


    @PostMapping(value = "/device/getInspectPlanByPage")
    public ResultModel getInspectPlanByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectPlanService.getInspectPlanByPage(pageEntity)) ;
    }
    @PostMapping(value = "/device/getInspectID")
    public ResultModel getInspectId(PageEntity pageEntity)  {
        return ResultModel.success(inspectPlanService.getInspectId(pageEntity));
    }


    @PostMapping("/device/addInspectPlan")
    public ResultModel addInspectPlan(@RequestBody InspectPlanNewDto inspectPlanNewDto){
        InspectPlanDto inspectPlanDto=new InspectPlanDto();
        inspectPlanDto.setInspectBy(inspectPlanNewDto.getInspectBy());
        inspectPlanDto.setInspectDesc(inspectPlanNewDto.getInspectDesc());
        inspectPlanDto.setId(inspectPlanNewDto.getId());
        inspectPlanDto.setCategory(inspectPlanNewDto.getCategory());
        inspectPlanDto.setCreateBy(inspectPlanNewDto.getCreateBy());
        inspectPlanDto.setDueTime(inspectPlanNewDto.getDueTime());
        inspectPlanDto.setIdentification(inspectPlanNewDto.getIdentification());
        inspectPlanDto.setInspectID(inspectPlanNewDto.getInspectID());
        inspectPlanDto.setIsValid(inspectPlanNewDto.isIsValid());
        inspectPlanDto.setPeriod(inspectPlanNewDto.getPeriod());
        inspectPlanDto.setPlanType(inspectPlanNewDto.getPlanType());
        inspectPlanDto.setTemplateID(inspectPlanNewDto.getTemplateID());
        inspectPlanDto.setUpdateBy(inspectPlanNewDto.getUpdateBy());
        String newtimes = String.join(",", Arrays.stream(inspectPlanNewDto.getTimes()).toArray(String[]::new));
        inspectPlanDto.setTimes(newtimes);
        inspectPlanService.addInspectPlan(inspectPlanDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectPlan")
    public ResultModel delInspectPlan(@RequestBody InspectPlanDto inspectPlanDto){
        inspectPlanService.delInspectPlan(inspectPlanDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectPlan")
    public ResultModel updateInspectPlan(@RequestBody InspectPlanDto inspectPlanDto){
        inspectPlanService.updateInspectPlan(inspectPlanDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @PostMapping("/device/receiveInspectSubItem")
    public ResultModel receiveInspectSubItem(@RequestBody InspectPlanNewDto inspectPlanNewDto){
        InspectPlanDto inspectPlanDto=new InspectPlanDto();
        inspectPlanDto.setInspectBy(inspectPlanNewDto.getInspectBy());
        inspectPlanDto.setInspectDesc(inspectPlanNewDto.getInspectDesc());
        inspectPlanDto.setId(inspectPlanNewDto.getId());
        inspectPlanDto.setCategory(inspectPlanNewDto.getCategory());
        inspectPlanDto.setCreateBy(inspectPlanNewDto.getCreateBy());
        inspectPlanDto.setDueTime(inspectPlanNewDto.getDueTime());
        inspectPlanDto.setIdentification(inspectPlanNewDto.getIdentification());
        inspectPlanDto.setInspectID(inspectPlanNewDto.getInspectID());
        inspectPlanDto.setIsValid(inspectPlanNewDto.isIsValid());
        inspectPlanDto.setPeriod(inspectPlanNewDto.getPeriod());
        inspectPlanDto.setPlanType(inspectPlanNewDto.getPlanType());
        inspectPlanDto.setTemplateID(inspectPlanNewDto.getTemplateID());
        inspectPlanDto.setUpdateBy(inspectPlanNewDto.getUpdateBy());
        String newtimes = String.join(",", Arrays.stream(inspectPlanNewDto.getTimes()).toArray(String[]::new));
        inspectPlanDto.setTimes(newtimes);
        inspectPlanService.receiveInspectSubItem(inspectPlanDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @PostMapping("/device/exportInspectPlan")
    public void exportInspectPlan(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectPlanEx> inspectPlanPage = inspectPlanService.getInspectPlanByPage(pageEntity);
        List<InspectPlanEx> inspectPlanExes = inspectPlanPage.getContent();
        List<InspectPlanExcelEx> inspectPlanExcelExes=inspectPlanExes.stream().map(item->{
            InspectPlanExcelEx excel =new InspectPlanExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectPlan接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), InspectPlanExcelEx.class).sheet("sheet1").doWrite(inspectPlanExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), InspectPlanExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(inspectPlanExcelExes);
    }

    @ApiOperation("获取巡检人列表")
    @PostMapping("/device/getInspectBy")
    public ResultModel getInspectByList() {
        return ResultModel.success(inspectPlanService.getInspectByList());
    }
}